//
//  TJLanguageSettingController.m
//  Teleport
//
//  Created by hc-101 on 2020/6/19.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJLanguageSettingController.h"
#import "TJLanguageSetingCell.h"
 

@interface TJLanguageSettingController ()

@property (strong, nonatomic) NSArray *datas;
@property (copy, nonatomic) NSString *language;
@property (strong, nonatomic) NSIndexPath *lastIndexPath;


@end

@implementation TJLanguageSettingController

static NSString * const languageSetingCell = @"languageSetingCell";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    

    _language = TJGlobalFunc.language;

    [self addBackButton];
    self.navigationBar.title = @"设置语言".localizedString;
    self.navigationBar.rightBarButton = [TJButtonBarButton barButtonWithTitle:@"保存".localizedString color:UIColor.blackColor target:self action:@selector(didClickSaveButton)];
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = 60;
    [self.tableView registerClass:TJLanguageSetingCell.self forCellReuseIdentifier:languageSetingCell];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
}

- (void)didClickSaveButton {
    [TJGlobalFunc setLanguage:_language];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJLanguageSetingCell *cell = [tableView dequeueReusableCellWithIdentifier:languageSetingCell forIndexPath:indexPath];
    NSDictionary *dict = self.datas[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.text = dict[@"text"];
    if ([dict[@"language"] isEqualToString:TJGlobalFunc.language]) {
        _lastIndexPath = indexPath;
        cell.tj_isSelect = YES;
    } else {
        cell.tj_isSelect = NO;
    }
    
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    TJLanguageSetingCell *lastCell = [tableView cellForRowAtIndexPath:_lastIndexPath];
    lastCell.tj_isSelect = NO;
    
    TJLanguageSetingCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.tj_isSelect = YES;
    
    _lastIndexPath = indexPath;
    
    _language = self.datas[indexPath.row][@"language"];
    
    NSDictionary *dict = self.datas[indexPath.row];
    if (_selectComplete) _selectComplete(dict[@"text"]);
 
}

- (void)popViewController {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (NSArray *)datas {
    if (!_datas) {
        _datas = @[
                   @{@"text": @"简体中文".localizedString, @"language": Simplified},
                   @{@"text": @"English", @"language": English},
                   ];
    }
    return _datas;
}

 

@end

